package org.wx.zk.comm;

import lombok.extern.slf4j.Slf4j;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.slf4j.Logger;

import java.util.concurrent.CountDownLatch;

/**
 * @description: zk 连接的 watch 事件
 * @author: wangxun
 * @createDate: 2021/3/23
 * @version: 1.0
 */
@Slf4j
public class DefaultWatcher implements Watcher {

    /**
     * 门栓 获取zk的时候阻塞，等到连接成功，放行
     */
    private CountDownLatch cd ;

    public CountDownLatch getCd() {
        return cd;
    }

    public void setCd(CountDownLatch cd) {
        this.cd = cd;
    }

    /**
     * 根据不同事件状态去做业务处理
     * @param event
     */
    @Override
    public void process(WatchedEvent event) {
        switch (event.getState()) {
            case Unknown:
                break;
            case Disconnected:

                break;
            case NoSyncConnected:
                break;
            case SyncConnected:
                // 成功连接放行
                log.info("SyncConnected countDown");
                cd.countDown();
                break;
            case AuthFailed:
                break;
            case ConnectedReadOnly:
                break;
            case SaslAuthenticated:
                break;
            case Expired:
                break;
            case Closed:
                log.info("zk client is closed....");
                break;
        }
    }
}
